package com.code.leetcode.offer;

import com.code.leetcode.utils.TreeNode;

import java.util.*;

/**
 * @author codeDorado
 * @version 1.0
 * @date 2020/8/1 11:04
 */
public class The32_1 {
    Queue<TreeNode> deque = new LinkedList<>();
    List<Integer> list = new ArrayList<>();
    int index;

    public int[] levelOrder(TreeNode root) {
        if (root == null) return new int[]{};
        deque.add(root);

        while (!deque.isEmpty()) {
            int size = deque.size();
            for (int i = 0; i < size; i++) {
                TreeNode node = deque.poll();
                list.add(node.val);
                if (node.left != null) deque.add(node.left);
                if (node.right != null) deque.add(node.right);
            }
        }
        int[] ints = new int[list.size()];

        list.forEach(num -> {
            ints[index++] = num;
        });
        return ints;
    }
}
